/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elaborate.tag_analysis.client.actions.ui;

import elaborate.tag_analysis.client.actions.ui.multiplefileselector.FileSelectionButtonCellEditor;
import elaborate.tag_analysis.client.actions.ui.multiplefileselector.FileSelectionButtonCellRenderer;
import elaborate.tag_analysis.client.actions.ui.multiplefileselector.MultipleFileSelectionConfEntry;
import elaborate.tag_analysis.client.actions.ui.multiplefileselector.MultipleFileSelectionModel;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.List;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

/**
 *
 * @author lendle
 */
public class MultipleFileSelectionPanel extends javax.swing.JPanel {
    private MultipleFileSelectionModel model=null;

    public JTable getTableFileSelection() {
        return tableFileSelection;
    }
    
    /**
     * Creates new form MultipleFileSelectionPanel
     */
    public MultipleFileSelectionPanel() {
        initComponents();
        model=new MultipleFileSelectionModel();
        this.tableFileSelection.setModel(model);
        this.tableFileSelection.getColumnModel().getColumn(2).setResizable(false);
        this.tableFileSelection.getColumnModel().getColumn(2).setMaxWidth(30);
        this.tableFileSelection.getColumnModel().getColumn(2).setPreferredWidth(30);
        this.tableFileSelection.getColumnModel().getColumn(0).setMaxWidth(200);
        this.tableFileSelection.getColumnModel().getColumn(0).setPreferredWidth(200);
        this.tableFileSelection.getColumnModel().getColumn(2).setCellRenderer(new FileSelectionButtonCellRenderer());
        this.tableFileSelection.getColumnModel().getColumn(2).setCellEditor(new FileSelectionButtonCellEditor());
        SwingUtilities.invokeLater(new Runnable(){

            @Override
            public void run() {
                tableFileSelection.updateUI();
            }
        });
    }
    
    public void setMultipleFileSelectionConfEntries(List<MultipleFileSelectionConfEntry> multipleFileSelectionConfEntries){
        this.model.setMultipleFileSelectionConfEntries(multipleFileSelectionConfEntries);
        SwingUtilities.invokeLater(new Runnable(){

            @Override
            public void run() {
                tableFileSelection.updateUI();
            }
        });
    }

    public File getSelectedFile(String id){
        for(MultipleFileSelectionConfEntry entry : this.model.getMultipleFileSelectionConfEntries()){
            if(id.equals(entry.getId())){
                return entry.getLastSelectedFile();
            }
        }
        return null;
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tableFileSelection = new javax.swing.JTable(){
            public String getToolTipText(MouseEvent event){
                int row=this.rowAtPoint(event.getPoint());
                int column=this.columnAtPoint(event.getPoint());
                if(column==1){
                    MultipleFileSelectionConfEntry entry=model.getMultipleFileSelectionConfEntries().get(row);
                    if(entry.getLastSelectedFile()!=null){
                        return entry.getLastSelectedFile().getAbsolutePath();
                    }
                }
                return super.getToolTipText(event);
            }
        };

        tableFileSelection.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Title", "File Path", "..."
            }
        ));
        jScrollPane1.setViewportView(tableFileSelection);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableFileSelection;
    // End of variables declaration//GEN-END:variables
}
